// -------- PIN SETUP -------- #define BUTTON_PIN D8 // button input #define LED_PIN D2 // yellow LED (active LOW or HIGH — we'll handle) // -------- TIMING CONFIG -------- const unsigned long debounceDelay = 50; // debounce time const unsigned long longPressTime = 1000; // long press threshold const unsigned long doublePressGap = 400; // max gap for double press // -------- STATE VARIABLES -------- bool buttonState = LOW; // current stable state bool lastButtonReading = LOW; // last raw reading unsigned long lastDebounceTime = 0; unsigned long pressStartTime = 0; unsigned long lastReleaseTime = 0; int pressCount = 0; int totalAmount = 0; // -------- SETUP -------- void setup() { Serial.begin(115200); pinMode(BUTTON_PIN, INPUT_PULLDOWN); // using your proven setup pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); // LED OFF initially } // -------- LOOP -------- void loop() { int reading = digitalRead(BUTTON_PIN); // -------- DEBOUNCE -------- if (reading != lastButtonReading) { lastDebounceTime = millis(); // reset debounce timer } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; // -------- BUTTON PRESSED -------- if (buttonState == HIGH) { pressStartTime = millis(); // start timing } // -------- BUTTON RELEASED -------- else { unsigned long pressDuration = millis() - pressStartTime; // -------- LONG PRESS -------- if (pressDuration >= longPressTime) { handleLongPress(); // ₹5 pressCount = 0; // reset count (important) } else { // short press → count for single/double pressCount++; lastReleaseTime = millis(); } } } } // -------- SINGLE / DOUBLE DECISION -------- if (pressCount > 0 && (millis() - lastReleaseTime) > doublePressGap) { if (pressCount == 1) { handleSinglePress(); // ₹1 } else if (pressCount == 2) { handleDoublePress(); // ₹2 } pressCount = 0; // reset after handling } lastButtonReading = reading; } // -------- ACTIONS -------- // SINGLE PRESS → ₹1 void handleSinglePress() { totalAmount += 1; Serial.print("Single Press | +1 | Total: "); Serial.println(totalAmount); // single blink digitalWrite(LED_PIN, HIGH); delay(120); digitalWrite(LED_PIN, LOW); } // DOUBLE PRESS → ₹2 void handleDoublePress() { totalAmount += 2; Serial.print("Double Press | +2 | Total: "); Serial.println(totalAmount); // double blink for (int i = 0; i < 2; i++) { digitalWrite(LED_PIN, HIGH); delay(120); digitalWrite(LED_PIN, LOW); delay(120); } } // LONG PRESS → ₹5 void handleLongPress() { totalAmount += 5; Serial.print("Long Press | +5 | Total: "); Serial.println(totalAmount); // long glow digitalWrite(LED_PIN, HIGH); delay(400); digitalWrite(LED_PIN, LOW); }